package com.code.leetcode.before50;

import com.code.leetcode.utils.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/25 19:29
 */
public class The102 {

    Deque<TreeNode> deque = new ArrayDeque<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        if (root == null) return lists;
        deque.addLast(root);
        while (!deque.isEmpty()) {
            int len = deque.size();
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                TreeNode node = deque.pollFirst();
                list.add(node.val);
                if (node.left != null) deque.addLast(node.left);
                if (node.right != null) deque.addLast(node.right);
            }
            lists.add(list);
        }
        return lists;
    }
}
